package org.ft.fileConvert.utils;

import lombok.extern.slf4j.Slf4j;
import org.ofd.render.OFDRender;
import org.ofdrw.layout.OFDDoc;
import org.ofdrw.layout.edit.Attachment;
import org.ofdrw.reader.OFDReader;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * Created by Chen Tianzhong in 2022/8/11
 */
@Component
@Slf4j
public class OfdAttachmentUtil {

    /**
     * 加入替换ofd附件对象
     * @param ofdFilePath
     * @param attachFilepath
     * @param attachmentName
     * @return
     * @throws IOException
     */
    public String attachFileIntoOFD(String ofdFilePath, String attachFilepath, String attachmentName) throws IOException {
        Path ofdIn = Paths.get(ofdFilePath);
        Path ofdout = Paths.get(ofdFilePath + ".tmp");
        Path attachPath = Paths.get(attachFilepath);
        try (OFDReader reader = new OFDReader(ofdIn);
             OFDDoc ofdDoc = new OFDDoc(reader, ofdout)) {
            // 加入附件文件
            ofdDoc.addAttachment(new Attachment(attachmentName, attachPath));
        }
        return ofdFilePath;
    }
}
